--if not ( RES_DISTRIB == 3 ) then Error("Please Install Resource Distribution 2 Addon.'" ) return end

TOOL.Category = 'Asteroid Mining Tool'
TOOL.Name = '#Isotope Storage Modules'
TOOL.Command = nil
TOOL.ConfigName = ''
--if (CLIENT and GetConVarNumber("RD.UseLSTab") == 1) then TOOL.Tab = "Life Support" end
if (CLIENT and GetConVarNumber("CAF_UseTab") == 1) then TOOL.Tab = "Custom Addon Framework" end

TOOL.ClientConVar['type'] = 'storage_ice_product_product'
TOOL.ClientConVar['model'] = 'models/props_wasteland/laundry_washer003.mdl'

cleanup.Register('am_store_iceproduct_mod')

if ( CLIENT ) then
	language.Add( 'Tool_am_store_iceproduct_mod_name', 'Life Support Resource modules' )
	language.Add( 'Tool_am_store_iceproduct_mod_desc', 'Create Life Support Resource modules attached to any surface.' )
	language.Add( 'Tool_am_store_iceproduct_mod_0', 'Left-Click: Spawn a Device.  Right-Click: Repair Device.' )

	language.Add( 'Undone_am_store_iceproduct_mod', 'Ice Product Storage Undone' )
	language.Add( 'Cleanup_am_store_iceproduct_mod', 'AM: Ice Product Storage Modules' )
	language.Add( 'Cleaned_am_store_iceproduct_mod', 'Cleaned up all Ice Product Storage Modules' )
	language.Add( 'SBoxLimit_am_store_iceproduct_mod', 'Maximum Ice Product Storage Modules Reached' )
end

local am_store_iceproduct_mod = {}
if (SERVER) then
	am_store_iceproduct_mod.storage_ice_product = function( ply, ent, system_type, system_class, model )
		local hash = {}
		local angles = ent:GetAngles()
		
		ent:SetAngles(Vector(0, 0, 0))
		
		local min, max = ent:WorldSpaceAABB()
		local size = max-min
		local square = (max.x - min.x) * (max.y - min.y)
		local volume = square * (max.z - min.z)
		
		ent:SetAngles(angles)
		
		Msg("Volume: "..tostring(volume).."\n")
		
		hash.size = volume/7.59
		hash.m3 = (volume/(61023.7441/500))
		
		local maxhealth = math.Round(hash.size/6)
		local mass = math.Round(hash.size/40) 		

		RD.AddResource(ent, "Oxygen Isotopes", math.Round(hash.m3))
		RD.AddResource(ent, "Hydrogen Isotopes", math.Round(hash.m3))
		--RD.AddResource(ent, "Helium Isotopes", math.Round(hash.m3))
		RD.AddResource(ent, "Nitrogen Isotopes", math.Round(hash.m3))
		--RD.AddResource(ent, "Liquid Ozone", math.Round(hash.m3))
		--RD.AddResource(ent, "heavy water", math.Round(hash.m3))
		--RD.AddResource(ent, "Strontium Clathrates", math.Round(hash.m3))
		
		hash.size = math.Round(hash.size)
		
		--LS_RegisterEnt(ent, "Ice Product Storage")
		--RD.RegisterNonStorageDevice(ent, "Ice Product Storage")
		
		return hash, maxhealth, mass  	
	end
end

local storage_models = {
	--{ 'Huge', 'models/Spacebuild/resourcecachehuge.mdl', 'storage_ice_product' },
	--{ 'Massive', 'models/Spacebuild/resourcecachemassive.mdl', 'storage_ice_product' },
	{ 'Small Cache', 'models/ce_ls3additional/resource_cache/resource_cache_small.mdl', 'storage_ice_product' },
	{ 'Medium Cache', 'models/ce_ls3additional/resource_cache/resource_cache_medium.mdl', 'storage_ice_product' },
	{ 'Large Cache', 'models/ce_ls3additional/resource_cache/resource_cache_large.mdl', 'storage_ice_product' },
	{ 'Large Tank', 'models/props_wasteland/coolingtank02.mdl', 'storage_ice_product' },
	{ 'Medium Tank', 'models/props_wasteland/horizontalcoolingtank04.mdl', 'storage_ice_product' },	
	{ 'Small Tank', 'models/props_wasteland/laundry_washer001a.mdl', 'storage_ice_product' }	
}

RD2_ToolRegister( TOOL, storage_models, nil, "am_store_iceproduct_mod", 30, am_store_iceproduct_mod )
